package org.mihalovic.PES.Core.Graph;


/** Directed weighted edge */
public class Edge implements Comparable<Edge>
{
    private Node sourceNode;
    private Node targetNode;

    private double weight;


    /**
     * Creates directed edge with default weight of 1.
     * 
     * @param sourceNode
     * @param targetNode
     */
    public Edge(Node sourceNode, Node targetNode){
        this(sourceNode, targetNode, 1.0);
    }

    /**
     * Directed edge with given weight
     *
     * @param sourceNode vertex from which this edge comes out
     * @param targetNode vertex to which this edges points
     * @param weight weight of the edge
     */
    public Edge(Node sourceNode, Node targetNode, double weight)
    {
        this.sourceNode = sourceNode;
        this.targetNode = targetNode;
        this.weight = weight;
    }


    public Node getSourceNode() { return sourceNode; }
    public Node getTargetNode() { return targetNode; }

    public double getWeight() { return weight; }
    public void setWeight(double weight){
        this.weight = weight;
    }


    @Override
    public int hashCode() {
        return sourceNode.getName().hashCode()*10000 + targetNode.getName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || getClass() != obj.getClass()) return false;

        final Edge other = (Edge) obj;
        return (other.getSourceNode().equals(getSourceNode()) &&
                other.getTargetNode().equals(getTargetNode()));
    }

    @Override
    public String toString(){
        return sourceNode.getName() + " --> " + targetNode.getName();
    }

    /**
     * Provides ordering based on edge weights.
     *
     * Note: this class has a natural ordering that is inconsistent with equals.
     */
    public int compareTo(Edge edge) {
        if(this.weight > edge.weight)
            return 1;
        if(this.weight == edge.weight)
            return 0;
        else return -1;
    }
}
